package com.wangtao.firstapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.jcodecraeer.xrecyclerview.ProgressStyle;
import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.wangtao.firstapp.R;
import com.wangtao.firstapp.adapters.RecyclerViewAdapter;
import com.wangtao.firstapp.inter.OnRVItemClickListener;
import com.wangtao.firstapp.inter.OnRVItemLongClickListener;
import com.wangtao.firstapp.modles.CommunityArticlesData;
import com.wangtao.firstapp.utils.HttpUtils;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;
import java.util.List;

/**
 * 文章页面下新鲜原创模块二级页面
 * Created by lanouhn on 16/9/2.
 */
public class NewestActivity extends AppCompatActivity implements OnRVItemClickListener, OnRVItemLongClickListener {
    private ImageView iv_newest_title_back; // 标题栏中返回
    private XRecyclerView xrecyclerview_newest;// 用于加载列表的RecyclerView
    private List<CommunityArticlesData.DataBean.TopicBean> articlesdata = new ArrayList<>();
    private List<CommunityArticlesData.DataBean.TopicBean> datas = new ArrayList<>();
    private RecyclerViewAdapter recyclerViewAdapter;
    private LinearLayoutManager llm;
    int page = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_newest);

        xrecyclerview_newest = (XRecyclerView) findViewById(R.id.xrecyclerview_newest);

        llm = new LinearLayoutManager(NewestActivity.this, LinearLayoutManager.VERTICAL, false);
        xrecyclerview_newest.setLayoutManager(llm);

        xrecyclerview_newest.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader);
        xrecyclerview_newest.setArrowImageView(R.drawable.ic_loading_rotate);
        xrecyclerview_newest.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page = 0;
                getData();
            }

            @Override
            public void onLoadMore() {
                page++;
                getData();
            }
        });

        initView();
        initEvent();
        getData();
    }

    private void initView() {
        iv_newest_title_back = (ImageView) findViewById(R.id.iv_newest_title_back);
    }

    private void initEvent() {
        iv_newest_title_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    private void getData() {
        String url = "http://open4.bantangapp.com/topics/topic/listByUsers";
        String param = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2559956&oauth_token=6beffa8c92276d19d3c23acb596024e6&track_deviceid=866962021116243&track_device_info=m1+note&channel_name=meizu&app_installtime=1471923362&app_versions=5.8.7&os_versions=5.1&screensize=1080&v=19&page=" + page + "&pagesize=20&sort_type=0";
        try {
            HttpUtils.doPostAsyn(url, param, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson = new Gson();
                    datas = gson.fromJson(result, CommunityArticlesData.class).getData().getTopic();
                    if (datas.size() > 0) {
                        handler.sendEmptyMessage(1);
                    } else {
                        handler.sendEmptyMessage(0);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    // Toast.makeText(NewestActivity.this, "网络访问失败", Toast.LENGTH_SHORT).show();
                    break;
                case 1:
//                    recyclerViewAdapter = new RecyclerViewAdapter(NewestActivity.this, articlesdata);
//                    xrecyclerview_newest.setAdapter(recyclerViewAdapter);
//                    LinearLayoutManager llm = new LinearLayoutManager(NewestActivity.this);
//                    xrecyclerview_newest.setLayoutManager(llm);
//                    recyclerViewAdapter.setClickListener(NewestActivity.this);
//                    recyclerViewAdapter.setLongClickListener(NewestActivity.this);

                    if (page == 0) {
                        articlesdata.clear();
                        articlesdata.addAll(datas);
                        recyclerViewAdapter = new RecyclerViewAdapter(NewestActivity.this, articlesdata);// 初始化适配器
                        xrecyclerview_newest.setAdapter(recyclerViewAdapter);// 给RecyclerView设置适配器
                        xrecyclerview_newest.refreshComplete();
                        datas.clear();
                    } else {
                        articlesdata.addAll(datas);
                        recyclerViewAdapter.notifyDataSetChanged();
                        xrecyclerview_newest.loadMoreComplete();
                        datas.clear();
                    }
                    recyclerViewAdapter.setClickListener(NewestActivity.this);// 本Activity中使用回调接口
                    recyclerViewAdapter.setLongClickListener(NewestActivity.this);// 本Activity中使用回调接口

                    break;
            }
        }
    };

    @Override
    public void OnRVItemClickListener(int position) {
        // 图文详情的网址
        String address = "http://open4.bantangapp.com/topic/newInfo";
        String parameter = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2573914&oauth_token=12977829338e5f953e3d24bcef502b6a&track_deviceid=866479023806507&track_device_info=x600&channel_name=leshi&app_installtime=1471853508" +
                "&app_versions=5.8.6&os_versions=5.0.2&screensize=1080&v=18&id=" + articlesdata.get(position).getId() + "&trace_id=&relate_pid=&statistics_uv=1&is_night=0";
        // 大家都在看 的网址
        String address1 = "http://open4.bantangapp.com/topics/topic/relationRec";
        String parameter1 = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2573914&oauth_token=12977829338e5f953e3d24bcef502b6a&track_deviceid=866479023806507&track_device_info=x600&channel_name=leshi&app_installtime=1471853508" +
                "&app_versions=5.8.6&os_versions=5.0.2&screensize=1080&v=18&id=" + articlesdata.get(position).getId();
        // 评论  网址
        String address2 = "http://open4.bantangapp.com/topics/comment/list";
        String parameter2 = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2573914&oauth_token=12977829338e5f953e3d24bcef502b6a&track_deviceid=866479023806507&track_device_info=x600&channel_name=leshi&app_installtime=1471853508" +
                "&app_versions=5.8.6&os_versions=5.0.2&screensize=1080&v=18&page=1&pagesize=20&id=" + articlesdata.get(position).getId();
        Intent intent_click = new Intent(NewestActivity.this, NewestSecondryActivity.class);
        intent_click.putExtra("url", address);
        intent_click.putExtra("url1", address1);
        intent_click.putExtra("url2", address2);
        intent_click.putExtra("param", parameter);
        intent_click.putExtra("param1", parameter1);
        intent_click.putExtra("param2", parameter2);
        startActivity(intent_click);
    }

    @Override
    public void OnRVItemLongClickListener(int position) {
        // 图文详情的网址
        String address = "http://open4.bantangapp.com/topic/newInfo";
        String parameter = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2573914&oauth_token=12977829338e5f953e3d24bcef502b6a&track_deviceid=866479023806507&track_device_info=x600&channel_name=leshi&app_installtime=1471853508" +
                "&app_versions=5.8.6&os_versions=5.0.2&screensize=1080&v=18&id=" + articlesdata.get(position).getId() + "&trace_id=&relate_pid=&statistics_uv=1&is_night=0";
        // 大家都在看 的网址
        String address1 = "http://open4.bantangapp.com/topics/topic/relationRec";
        String parameter1 = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2573914&oauth_token=12977829338e5f953e3d24bcef502b6a&track_deviceid=866479023806507&track_device_info=x600&channel_name=leshi&app_installtime=1471853508" +
                "&app_versions=5.8.6&os_versions=5.0.2&screensize=1080&v=18&id=" + articlesdata.get(position).getId();
        // 评论  网址
        String address2 = "http://open4.bantangapp.com/topics/comment/list";
        String parameter2 = "app_id=com.jzyd.BanTang&client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=2573914&oauth_token=12977829338e5f953e3d24bcef502b6a&track_deviceid=866479023806507&track_device_info=x600&channel_name=leshi&app_installtime=1471853508" +
                "&app_versions=5.8.6&os_versions=5.0.2&screensize=1080&v=18&page=1&pagesize=20&id=" + articlesdata.get(position).getId();
        Intent intent_click = new Intent(NewestActivity.this, NewestSecondryActivity.class);
        intent_click.putExtra("url", address);
        intent_click.putExtra("url1", address1);
        intent_click.putExtra("url2", address2);
        intent_click.putExtra("param", parameter);
        intent_click.putExtra("param1", parameter1);
        intent_click.putExtra("param2", parameter2);
        startActivity(intent_click);
    }
}
